<?php
namespace app\index\controller;
use think\Controller;
use think\facade\Log;

class Wxpay extends Controller{



    function xmlToArray($xml) {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
        $val = json_decode(json_encode($xmlstring), true);
        return $val;
    }

    public function toArray($xml) {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result= json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }



    /**
     * 下订单回调
     * @return [type] [description]
     */
    public function paynotify()
    {
        $xml = file_get_contents('php://input', 'r'); //接收微信参数
        //将服务器返回的XML数据转化为数组
        $dataArr = $this->toArray($xml);
        Log::debug("paynotify::::::::::" . json_encode($dataArr));
        //$dataArr   = input('post.');
        $order_sn  = $dataArr['out_trade_no'];          // 订单单号
        $openid    = $dataArr['openid'];                  // 付款人openID
        $total_fee = $dataArr['total_fee'];            // 付款金额
        $transaction_id = $dataArr['transaction_id'];  // 微信支付流水号

        //查询主订单
        $order = db('order')->where('order_no', $order_sn)->field('order_id,order_no,company_id,user_id,money,status,transaction_no,pay_fee')->find();
        if(empty($order)){
            $this->ajaxReturn(null, '订单为空', 204);
        }
        if($order['status']==2){
            $this->ajaxReturn(null, '已支付', 204);
        }

        $res = db('order')->where('order_no', $order_sn)->update(['status'=>2, 'transaction_no'=>$transaction_id, 'pay_fee'=>$total_fee]);
        $res = db('company')->where('id', $order['company_id'])->setInc('account', $total_fee);
        if($res!==false){
            $this->ajaxReturn($res, '支付成功', 200);
        }else{
            $this->ajaxReturn(null, '支付失败', 204);
        }
    }



    /**
     * 下订单回调
     * @return [type] [description]
     */
    public function testpaynotify()
    {
        $order_sn = $_GET['order_no'];
                // 订单单号
        $pass = $_GET['pass'];
        if ($pass != 'dongdongidea') {
            die('接口访问错误');
        }
        //查询主订单
        $order = db('order')->where('order_no', $order_sn)->field('order_id,order_no,company_id,user_id,money,status,transaction_no,pay_fee')->find();
        if(empty($order)){
            $this->ajaxReturn(null, '订单为空', 204);
        }
        if($order['status']==2){
            $this->ajaxReturn(null, '已支付', 204);
        }

        $res = db('order')->where('order_no', $order_sn)->update(['status'=>2, 'transaction_no'=> 11112111, 'pay_fee'=> $order['money']]);
        $res = db('company')->where('id', $order['company_id'])->setInc('account', $order['money']);
        if($res!==false){
            $this->ajaxReturn($res, '支付成功', 200);
        }else{
            $this->ajaxReturn(null, '支付失败', 204);
        }
    }

}

